﻿<?php
function getIp(){ 
     if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) 
         $ip = getenv("HTTP_CLIENT_IP"); 
     else 
         if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) 
             $ip = getenv("HTTP_X_FORWARDED_FOR"); 
         else 
             if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) 
                 $ip = getenv("REMOTE_ADDR"); 
             else 
                 if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) 
                     $ip = $_SERVER['REMOTE_ADDR']; 
                 else 
                     $ip = "unknown"; 
     return ($ip); 
}
$ssq=mysql_query("select * from ph_login where ip='".getIp()."' and user_name='".$_SESSION["managername"]."' order by id desc");
$srow=mysql_fetch_array($ssq);
?>
<script type="text/javascript">
function chekj(){
$.ajax({
  url: '/server.php?user_name=<?php echo $_SESSION["managername"];?>&login_time=<?php echo base64_encode($srow["cdate"]);?>',
  dataType: 'text',
  async:true,
  success: function(res){
        if(res=='yess'){
		alert("系统检测到当前帐户在别处同时登录");
		location.href='/quit.php';
		}
}
  
});
}
//关键在这里，Ajax定时访问服务端，不断获取数据 ，这里是1秒请求一次。
setInterval(function(){chekj();},10000);
</script>